package phonecompany.customer;

import java.io.Serializable;
import java.util.Calendar;
import java.util.Date;

import org.apache.log4j.BasicConfigurator;
import org.apache.log4j.Level;
import org.apache.log4j.Logger;

import phonecompany.phone.Phone;

/**
 * This class defines the Customer entity which contains the name , date of
 * birth, age and its phne.
 * 
 * @since Oct 24, 2011
 * @author <a href="mailto:zexpp5@gmail.com">Zexu.Lin</a>
 * @version $Id: Customer.java 28 2011-10-30 21:25:58Z zexpp5@gmail.com $
 * 
 */
public final class Customer implements Serializable {
	private CustomerName customerName;
	private Calendar dateOfBirth;
	private int age;
	private Phone phone;

	private Customer() {
	}

	/**
	 * A customer could only be created by known name and date of birth. This
	 * contructor will automatically calculate the age of the customer.
	 * 
	 * Constructor Method
	 * 
	 * @param customerName
	 * @param dateOfBirth
	 */
	public Customer(CustomerName customerName, Calendar dateOfBirth) {
		this.customerName = customerName;
		this.dateOfBirth = dateOfBirth;
		this.age = (int) ((Calendar.getInstance().getTimeInMillis() - this.dateOfBirth
				.getTimeInMillis()) / (1000 * 60 * 60 * 24 * 365));
	}

	/**
	 * To get the Phone issued to current Customer.
	 * 
	 * @return
	 * @description
	 * 
	 * @author <a href="mailto:zexpp5@gmail.com">Zexu.Lin</a>
	 * @update Oct 30, 2011 7:14:29 PM
	 */
	public Phone getPhone() {
		return phone;
	}

	/**
	 * Used the the customer is issued a Phone.
	 * 
	 * @param phone
	 * @description
	 * 
	 * @author <a href="mailto:zexpp5@gmail.com">Zexu.Lin</a>
	 * @update Oct 30, 2011 7:14:51 PM
	 */
	public void setPhone(Phone phone) {
		this.phone = phone;
	}

	/**
	 * To get the age of current customer.
	 * 
	 * @return
	 * @description
	 * 
	 * @author <a href="mailto:zexpp5@gmail.com">Zexu.Lin</a>
	 * @update Oct 30, 2011 7:15:21 PM
	 */
	public int getCustomerAge() {

		return age;
	}

	/**
	 * To get the name of current customer.
	 * 
	 * @return
	 * @description
	 * 
	 * @author <a href="mailto:zexpp5@gmail.com">Zexu.Lin</a>
	 * @update Oct 30, 2011 7:15:42 PM
	 */
	public CustomerName getCustomerName() {
		return customerName;
	}

	/**
	 * To get the date of birth of current customer.
	 * 
	 * @return
	 * @description
	 * 
	 * @author <a href="mailto:zexpp5@gmail.com">Zexu.Lin</a>
	 * @update Oct 30, 2011 7:15:58 PM
	 */
	public Calendar getDateOfBirth() {
		return dateOfBirth;
	}

	/**
	 * Overrided toString() method which will return the name and date of birth
	 * of current customer.l
	 * 
	 * @see java.lang.Object#toString()
	 */
	@Override
	public String toString() {
		return customerName + ":" + dateOfBirth.getTime();
	}

	/**
	 * overrided equals() method return true when customers have same name and
	 * age.
	 * 
	 * 
	 * @see java.lang.Object#equals(java.lang.Object)
	 */
	@Override
	public boolean equals(Object obj) {
		if (this.toString() == obj.toString())
			return true;

		return false;
	}

	/**
	 * To correspond the overrided equals method.
	 * 
	 * @see java.lang.Object#hashCode()
	 */
	@Override
	public int hashCode() {
		int hc = 17;
		hc = 37 * hc
				+ (this.toString() == null ? 0 : this.toString().hashCode());
		return hc;
	}

}
